package com.jack.leetcode.每日一题;

import java.util.ArrayList;
import java.util.List;

/***
 * @author zhangjie
 * @date 2020/4/22 20:28
 */
public class 二叉树的右视图 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        int length = 0;
        if (root == null) {
            return result;
        }
        dfs(root, result, length);
        return result;
    }

    private void dfs(TreeNode root, List<Integer> result, int length) {
        if (root == null) {
            return;
        }
        if (result.size() == length) {
            result.add(root.val);
        }
        dfs(root.right, result, length + 1);
        dfs(root.left, result, length + 1);
    }
}
